﻿//using System;
//using System.ComponentModel.DataAnnotations.Schema;
//using System.Data;
//using System.Data.Common;
//using System.Linq;
//using System.Linq.Expressions;
//using System.Reflection;
//using System.Text;
//using Dapper;

//namespace NiuX.Dapper.SqlServer;

//public static class DbConnectionExtensions
//{
//    public static void From<T>(this IDbConnection sqlConnection)
//    {

//    }
//}

//public class CommandSet<T>
//{
//    public IDbConnection Connection { get; set; }

//    public SqlProvider SqlProvider { get; set; }

//    public CommandSet(IDbConnection conn, SqlProvider sqlProvider)
//    {
//        Connection = conn;
//        SqlProvider = sqlProvider;
//        SqlProvider.SetContext.TableType = typeof(T);

//    }

//    public int Update(T entity)
//    {
//        var model = SqlProvider.ResolveUpdatingSql(entity);

//        return Connection.Execute(model.SqlString, model.Params);
//    }
//}

//public abstract class SqlProvider
//{
//    public SetContext SetContext { get; set; }

//    public abstract IResolvingSql ResolveUpdatingSql<T>(T entity);


//}


//public class SqlServerProvider : SqlProvider
//{
//    public override IResolvingSql ResolveUpdatingSql<T>(T entity)
//    {
//        throw new NotImplementedException();
//    }
//}

//public class ResolvingSqlModel
//{
//    public string SqlString { get; set; }

//    public DynamicParameters Params { get; set; }

//}



//public class SetContext
//{
//    public Type TableType { get; internal set; }

//}

//public class ExpressionResolver
//{
//    public static ResolvingSqlModel ResolveUpdate<T>(Expression<Func<T, T>> updateExpression)
//    {
//        var expression = new UpdatingExpression(updateExpression, SqlServerProviderOption.Instance);

//        return new ResolvingSqlModel
//        {
//            SqlString = expression.StringBuilder.ToString(),
//            Params = expression.Params
//        };

//    }
//}

//public class UpdatingExpression : ExpressionVisitor
//{
//    public string SqlString { get; set; }

//    public DynamicParameters Params { get; set; } = new DynamicParameters();

//    public StringBuilder StringBuilder { get; set; } = new();
//    public ProviderOption ProviderOption { get; set; }

//    private const string Prefix = "UPDATE_";


//    public UpdatingExpression(LambdaExpression updateExpression, ProviderOption providerOption)
//    {
//        ProviderOption = providerOption;
//    }

//    protected override Expression VisitMemberInit(MemberInitExpression node)
//    {
//        foreach (var memberAssignment in node.Bindings.Cast<MemberAssignment>())
//        {
//            if (StringBuilder.Length > 0)
//            {
//                StringBuilder.Append(",");
//            }

//            var member = memberAssignment.Member;

//            var paramName = member.Name;
//            var orginFieldName = member.GetCustomAttribute<ColumnAttribute>()?.Name ?? member.Name;
//            var fieldName = ProviderOption.CombineFieldName(orginFieldName);

//            switch (memberAssignment.Expression.NodeType)
//            {
//                case ExpressionType.Constant:
//                    var value = ((ConstantExpression)memberAssignment.Expression).Value;
//                    StringBuilder.Append(fieldName).Append('=').Append(ProviderOption.ParameterPrefix).Append(Prefix).Append(paramName);
//                    Params.Add(paramName, value);
//                    break;
//                case ExpressionType.MemberAccess:
//                    //((MemberExpression)memberAssignment.Expression).
//                    break;
//                default:
//                    throw new NotImplementedException();
//            }
//        }

//        return node;
//    }
//}

//public static class UpdatingExtensions
//{
//    public static object MemberToValue(this MemberExpression expression)
//    {
//        if (expression.Expression == null || expression.Expression.NodeType == ExpressionType.Constant)
//            return expression;

//        return expression.Expression.NodeType == ExpressionType.MemberAccess
//                        ? ((MemberExpression)expression.Expression).GetRootMember()
//                        : null;
//    }

//    //public static MemberExpression GetRootMember(this MemberExpression e)
//    //{

//    //}

//}

//public class ProviderOption
//{
//    public ProviderOption(char openQuote, char closeQuote, char parameterPrefix)
//    {
//        OpenQuote = openQuote;
//        CloseQuote = closeQuote;
//        ParameterPrefix = parameterPrefix;
//    }

//    public char OpenQuote { get; set; }

//    public char CloseQuote { get; set; }

//    public char ParameterPrefix { get; set; }

//    public string CombineFieldName(string field)
//    {
//        return OpenQuote + field + CloseQuote;
//    }
//}

//public class SqlServerProviderOption : ProviderOption
//{


//    public static SqlServerProviderOption Instance { get; set; } = new SqlServerProviderOption();

//    public SqlServerProviderOption() : base('[', ']', '@')
//    {
//    }
//}